package com.uenpay.im.ui.old.activity.mycollect;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.movement.MovementDetailActivity;
import com.uenpay.im.weight.MyDialog;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

public class MyCollectListActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private TextView middleText;
    private RefreshListView listView;
    private MyColletListAdapter adapter;
    private ArrayList<MyCollect> collects;
    private View loading;
    private TextView no_data_tv;
    private int page = 1;
    private CollectHandle mCollectHandle = new CollectHandle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_xlist_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        loading.setVisibility(View.VISIBLE);
        getData();
    }

    protected void initView() {
        no_data_tv = (TextView) findViewById(R.id.no_data_tv);
        no_data_tv.setText(getResources().getString(R.string.no_collect));
        middleText = (TextView) findViewById(R.id.title_textview);
        middleText.setText(getResources().getString(R.string.collect));
        listView = (RefreshListView) findViewById(R.id.discover_listview);
        loading = findViewById(R.id.loading);
        listView.setDividerHeight(0);
    }

    protected void initListener() {
        listView.setPullLoadEnable(true);
        listView.setRefreshListViewListener(this);
        //返回键
        findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                MyCollect myCollect = (MyCollect) arg0.getAdapter().getItem(
                        arg2);

                if (myCollect != null) {
                    Intent intent = new Intent();
                    if (myCollect.moduleid == 103) {
                        intent.setClass(MyCollectListActivity.this,
                                CollectContentDetailActivity.class);
                        intent.putExtra("my_collect", myCollect);
                    } else if (myCollect.moduleid == 113) {
                        intent.setClass(MyCollectListActivity.this,
                                MovementDetailActivity.class);
                        intent.putExtra("movementId", myCollect.targetid);
                    }
                    startActivity(intent);
                }

            }
        });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           int position, long id) {
                final MyCollect myCollect = (MyCollect) parent.getAdapter()
                        .getItem(position);

                if (myCollect != null) {
                    MyDialog.Builder alertDialog = new MyDialog.Builder(
                            MyCollectListActivity.this);
                    alertDialog.setTitle(R.string.dialog_title);
                    alertDialog.setMessage(getResources().getString(
                            R.string.delete_collect));
                    alertDialog.setPositiveButton(R.string.sure,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    dialog.dismiss();
                                    deleteCollect(myCollect.favoriteid);
                                }
                            });

                    alertDialog
                            .setNegativeButton(
                                    R.string.cancel,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();
                                        }
                                    });
                    alertDialog.create().show();
                }

                return true;
            }
        });
    }

    private void getData() {

        mCollectHandle.getCollectList(page,
                new CollectHandle.IGetCollectListResultListener() {

                    @Override
                    public void onGetCollectListResult(int status, String msg,
                                                       ArrayList<MyCollect> arraylist) {
                        myHandler.sendEmptyMessage(0);

                        if (page == 1) {
                            if (arraylist != null && arraylist.size() < 1) {
                                myHandler.sendEmptyMessage(1);
                                return;
                            }
                            if (adapter == null) {
                                adapter = new MyColletListAdapter(
                                        MyCollectListActivity.this, arraylist);
                                listView.setAdapter(adapter);
                            } else {
                                adapter.setData(arraylist);
                            }
                            collects = arraylist;
                            listView.setPullLoadEnable(true);
                        } else {
                            if (arraylist != null && arraylist.size() > 0) {
                                collects.addAll(arraylist);
                                adapter.addData(arraylist);
                            } else {
                                listView.setPullLoadEnable(false);
                            }
                        }

                    }
                });
    }

    private Handler myHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                onLoad();
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            } else if (msg.what == 1) {
                if (no_data_tv != null) {
                    no_data_tv.setVisibility(View.VISIBLE);
                }
            }
        }

        ;
    };

    private void deleteCollect(final int favorite_id) {
        loading.setVisibility(View.VISIBLE);
        mCollectHandle.deleteCollect(favorite_id, new CollectHandle.ICollectListener() {

            @Override
            public void onResult(int status, String msg) {
                myHandler.sendEmptyMessage(0);
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    for (int i = 0, size = collects.size(); i < size; i++) {
                        if (collects.get(i).favoriteid == favorite_id) {
                            collects.remove(i);
                            break;
                        }
                    }
                    adapter.setData(collects);
                } else if (TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });

    }

    private void onLoad() {
        listView.stopRefresh();
        listView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }
}
